package com.log.service.Impl;

import com.log.common.util.PageHelper;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.LogGoodsMapper;
import com.log.pojo.LogGoods;
import com.log.service.LogGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: 林永智
 * @Date: 2023/10/23/15:25
 */
@Service
public class LogGoodsServiceImpl implements LogGoodsService {
    @Autowired
    private LogGoodsMapper logGoodsMapper;
    //添加商品信息
    @Override
    public ResultVo insertLogGoods(LogGoods logGoods) {
        Example example = new Example(LogGoods.class);
        Example.Criteria criteria  = example.createCriteria();
        //
        logGoods.setLogGoodsInTime(new Date());
        logGoods.setLogGoodsOutTime(new Date());
        criteria.andEqualTo("logGoodsName",logGoods.getLogGoodsName());
        List<LogGoods> logGoods1 = logGoodsMapper.selectByExample(example);
        if (logGoods1.size()>0){
            return new ResultVo(ResStatus.NO,"货物已存在",null);
        }else {
            logGoodsMapper.insert(logGoods);
            return new ResultVo(ResStatus.OK,"货物添加成功",null);
        }
    }

    //通过id删除商品信息
    @Override
    public ResultVo deleteLogGoodsById(int id) {
        logGoodsMapper.deleteByPrimaryKey(id);
        return new ResultVo(ResStatus.OK,"该商品已经删除成功",null);
    }

    //修改商品信息
    @Override
    public ResultVo updateLogGoods(LogGoods logGoods) {
        logGoodsMapper.updateByPrimaryKeySelective(logGoods);
        return new ResultVo(ResStatus.OK,"该商品已经修改成功",logGoods);
    }
    //通过id查询商品信息
    @Override
    public ResultVo selectLogGoodsById(int id) {
        LogGoods logGoods = logGoodsMapper.selectByPrimaryKey(id);
        return new ResultVo(ResStatus.OK,"查询成功",logGoods);
    }

    @Override
    public ResultVo selectLogGoodsListByPage(int pageNum, int limit,String current_depot_id) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogGoods> list = null;
        //3.查询总数
        int count = 0;
        Example example = new Example(LogGoods.class);
        Example.Criteria criteria = example.createCriteria();
        if(current_depot_id==null||current_depot_id.equals(""))
        {
            //查询全部分页
            list = logGoodsMapper.selectLogGoodsListByPage(start,limit);
        }else{
           list = logGoodsMapper.selectLogGoodsListByPageandDepotId(start,limit,Integer.parseInt(current_depot_id));
            criteria.andEqualTo("logGoodsDepotId",current_depot_id);
        }
        count =  logGoodsMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogGoods>(count,pageCount,list));
    }

    @Override
    public ResultVo selectGoodsbyDepot(int log_depot_id) {
        Example example = new Example(LogGoods.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("logGoodsDepotId",log_depot_id);
        List<LogGoods> logGoods = logGoodsMapper.selectByExample(example);
        return new ResultVo(ResStatus.OK,"success",logGoods);
    }

}
